/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.api.agricultural.machinery.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.utils.excel.annotation.ExcelField.Type;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.api.agricultural.machinery.entity.AgriculturalMachinery;
import com.jeesite.modules.api.agricultural.machinery.service.AgriculturalMachineryService;
import com.jeesite.modules.sys.entity.Post;
import com.jeesite.modules.sys.utils.DictUtils;
import com.jeesite.modules.zx.question.bank.entity.TquestionBank;

/**
 * 农机管理Controller
 * @author zpp
 * @version 2020-03-21
 */
@Controller
@RequestMapping(value = "${adminPath}/api/agricultural/machinery/agriculturalMachinery")
public class AgriculturalMachineryController extends BaseController {

	@Autowired
	private AgriculturalMachineryService agriculturalMachineryService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public AgriculturalMachinery get(String id, boolean isNewRecord) {
		return agriculturalMachineryService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("api:agricultural:machinery:agriculturalMachinery:view")
	@RequestMapping(value = {"list", ""})
	public String list(AgriculturalMachinery agriculturalMachinery, Model model) {
		model.addAttribute("agriculturalMachinery", agriculturalMachinery);
		return "modules/api/agricultural/machinery/agriculturalMachineryList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("api:agricultural:machinery:agriculturalMachinery:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<AgriculturalMachinery> listData(AgriculturalMachinery agriculturalMachinery, HttpServletRequest request, HttpServletResponse response) {
		agriculturalMachinery.setPage(new Page<>(request, response));
		Page<AgriculturalMachinery> page = agriculturalMachineryService.findPage(agriculturalMachinery);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("api:agricultural:machinery:agriculturalMachinery:view")
	@RequestMapping(value = "form")
	public String form(AgriculturalMachinery agriculturalMachinery, Model model) {
		model.addAttribute("agriculturalMachinery", agriculturalMachinery);
		return "modules/api/agricultural/machinery/agriculturalMachineryForm";
	}

	/**
	 * 保存农机管理
	 */
	@RequiresPermissions("api:agricultural:machinery:agriculturalMachinery:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated AgriculturalMachinery agriculturalMachinery) {
		agriculturalMachineryService.save(agriculturalMachinery);
		return renderResult(Global.TRUE, text("保存农机管理成功！"));
	}
	
	/**
	 * 停用农机管理
	 */
	@RequiresPermissions("api:agricultural:machinery:agriculturalMachinery:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(AgriculturalMachinery agriculturalMachinery) {
		agriculturalMachinery.setStatus(AgriculturalMachinery.STATUS_DISABLE);
		agriculturalMachineryService.updateStatus(agriculturalMachinery);
		return renderResult(Global.TRUE, text("停用农机管理成功"));
	}
	
	/**
	 * 启用农机管理
	 */
	@RequiresPermissions("api:agricultural:machinery:agriculturalMachinery:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(AgriculturalMachinery agriculturalMachinery) {
		agriculturalMachinery.setStatus(AgriculturalMachinery.STATUS_NORMAL);
		agriculturalMachineryService.updateStatus(agriculturalMachinery);
		return renderResult(Global.TRUE, text("启用农机管理成功"));
	}
	
	/**
	 * 删除农机管理
	 */
	@RequiresPermissions("api:agricultural:machinery:agriculturalMachinery:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(AgriculturalMachinery agriculturalMachinery) {
		agriculturalMachineryService.delete(agriculturalMachinery);
		return renderResult(Global.TRUE, text("删除农机管理成功！"));
	}
	@RequestMapping(value = "importTemplate")
	public void importTemplate(HttpServletRequest request,HttpServletResponse response) {
		AgriculturalMachinery agriculturalMachinery=new AgriculturalMachinery();
		Page<AgriculturalMachinery> findPage = agriculturalMachineryService.findPage(agriculturalMachinery);
		List<AgriculturalMachinery> list = findPage.getList();
		String fileName = "农机信息据数据模板.xlsx";
		try(ExcelExport ee = new ExcelExport("农机信息数据", AgriculturalMachinery.class, Type.IMPORT)){
			ee.setDataList(list).write(response, fileName);
		}
	}
	@ResponseBody
	@PostMapping(value = "importData")
	public String importData(MultipartFile file, String updateSupport) {
		try {
			boolean isUpdateSupport = Global.YES.equals(updateSupport);
			String message = agriculturalMachineryService.importData(file, isUpdateSupport);
			return renderResult(Global.TRUE, "posfull:"+message);
		} catch (Exception ex) {
			return renderResult(Global.FALSE, "posfull:"+ex.getMessage());
		}
	}
	@RequestMapping(value = "exportData")
	public void exportData(AgriculturalMachinery entity, Boolean isAll, String ctrlPermi, HttpServletResponse response) {
		List<AgriculturalMachinery> list = agriculturalMachineryService.findList(entity);
		String fileName = "农机信息数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		try(ExcelExport ee = new ExcelExport("农机信息数据", AgriculturalMachinery.class)){
			ee.setDataList(list).write(response, fileName);
		}
	}
}